package com.woniu.core.aop;


import com.woniu.core.annotation.SystemControllerLog;
import com.woniu.core.common.HttpContextUtil;
import com.woniu.dao.LogDao;
import com.woniu.pojo.Log;
import com.woniu.pojo.User;
import com.woniu.service.LogService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.sql.SQLException;
import java.util.Date;

@Aspect
@Component
public class SystemLogAspect {

    @Resource
    private LogService logService;
    @Resource
    private LogDao logDao;
    /***
     * 定义controller切入点拦截规则，拦截SystemControllerLog注解的方法
     */
    @Pointcut("@annotation(com.woniu.core.annotation.SystemControllerLog)")
    public void controllerAspect(){}

    /***
     * 拦截控制层的操作日志
     * @param joinPoint
     * @return
     * @throws Throwable
     */
    @Around("controllerAspect()")
    public Object recordLog(ProceedingJoinPoint joinPoint) throws Throwable {

        // 获取方法签名
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        // 获取方法
        Method method = methodSignature.getMethod();
        //获取方法的注解
        SystemControllerLog systemControllerLog=method.getAnnotation(SystemControllerLog.class);

        String operateType = systemControllerLog.descrption();
        System.out.println("========================================"+operateType);
        Log systemLog = new Log();
        systemLog.setOp(operateType);

        //获取session中的用户
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        Subject subject = SecurityUtils.getSubject();
        User loginUser = (User) subject.getSession().getAttribute("loginUser");
        systemLog.setUid(loginUser.getUid());
        systemLog.setUname(loginUser.getUname());
        //获取方法执行前时间
        System.out.println("+++++++++++++++++++++++"+systemLog.getUname()+"++++++++++++++++++++++++++++++++++++");
        //获取ip地址
        String ip = HttpContextUtil.getIpAddress();
        System.out.println("+++++++++++++++++++++++"+ip+"++++++++++++++++++++++++++++++++++++");
        systemLog.setIp(ip);

        Object result = null;
        try {
            result=joinPoint.proceed();
            systemLog.setOp("正常");
        } catch (SQLException e) {
            // 相当于异常通知部分
            systemLog.setOp("失败");// 设置操作结果
        } finally {
            // 相当于最终通知
            systemLog.setLtime(new Date());// 设置操作日期
            logDao.insertSelective(systemLog);// 添加日志记录
        }
        return result ;
    }


}
